home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue3 / Games / xrick / !xrick / src / c / sysarg < prev    next >
Text File  |  2004-06-24  |  8KB  |  235 lines

  1. /*
  2.  * xrick/src/sysarg.c
  3.  *
  4.  * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
  5.  *
  6.  * The use and distribution terms for this software are contained in the file
  7.  * named README, which can be found in the root of this distribution. By
  8.  * using this software in any fashion, you are agreeing to be bound by the
  9.  * terms of this license.
  10.  *
  11.  * You must not remove this notice, or any other, from this software.
  12.  */
  13.  
  14. /*
  15.  * 20021010 added test to prevent buffer overrun in -keys parsing.
  16.  */
  17.  
  18. #include <stdlib.h>  /* atoi */
  19. #include <string.h>  /* strcasecmp */
  20.  
  21. #include <SDL.h>
  22.  
  23. #include "system.h"
  24. #include "config.h"
  25. #include "game.h"
  26.  
  27. #include "maps.h"
  28. #include "syssnd.h"
  29.  
  30. /* handle Microsoft Visual C (must come after system.h!) */
  31. #ifdef __MSVC__
  32. #define strcasecmp stricmp
  33. #endif
  34.  
  35. typedef struct {
  36.   char name[16];
  37.   int code;
  38. } sdlcodes_t;
  39.  
  40. static sdlcodes_t sdlcodes[SDLK_LAST] = {
  41. #include "sdlcodes.e"
  42. };
  43.  
  44. int sysarg_args_period = 0;
  45. int sysarg_args_map = 0;
  46. int sysarg_args_submap = 0;
  47. int sysarg_args_fullscreen = 0;
  48. int sysarg_args_zoom = 0;
  49. int sysarg_args_nosound = 0;
  50. int sysarg_args_vol = 0;
  51. char *sysarg_args_data = NULL;
  52.  
  53. /*
  54.  * Fail
  55.  */
  56. void
  57. sysarg_fail(char *msg)
  58. {
  59. #ifdef ENABLE_SOUND
  60.     printf("xrick [version #%s]: %s\nusage: xrick [<options>]\n<option> =\n  -h, -help : Display this information. -fullscreen : Run in fullscreen mode.\n    The default is to run in a window.\n  -speed <speed> : Run at speed <speed>. Speed must be an integer between 1\n    (fast) and 100 (slow). The default is %d\n  -zoom <zoom> : Display with zoom factor <zoom>. <zoom> must be an integer\n   between 1 (320x200) and %d (%d times bigger). The default is 2.\n  -map <map> : Start at map number <map>. <map> must be an integer between\n    1 and %d. The default is to start at map number 1\n  -submap <submap> : Start at submap <submap>. <submap> must be an integer\n    between 1 and %d. The default is to start at submap number 1 or, if a map\n    was specified, at the first submap of that map.\n  -keys <left>-<right>-<up>-<down>-<fire> : Override the default key\n    bindings (cf. KeyCodes)\n  -nosound : Disable sounds. The default is to play with sounds enabled.\n  -vol <vol> : Play sounds at volume <vol>. <vol> must be an integer\n    between 0 (silence) and %d (max). The default is to play sounds\n    at maximal volume (%d).\n", VERSION, msg, GAME_PERIOD, SYSVID_MAXZOOM, SYSVID_MAXZOOM, MAP_NBR_MAPS-1, MAP_NBR_SUBMAPS, SYSSND_MAXVOL, SYSSND_MAXVOL);
  61. #else
  62.     printf("xrick [version #%s]: %s\nusage: xrick [<options>]\n<option> =\n  -h, -help : Display this information. -fullscreen : Run in fullscreen mode.\n    The default is to run in a window.\n  -speed <speed> : Run at speed <speed>. Speed must be an integer between 1\n    (fast) and 100 (slow). The default is %d\n  -zoom <zoom> : Display with zoom factor <zoom>. <zoom> must be an integer\n   between 1 (320x200) and %d (%d times bigger). The default is 2.\n  -map <map> : Start at map number <map>. <map> must be an integer between\n    1 and %d. The default is to start at map number 1\n  -submap <submap> : Start at submap <submap>. <submap> must be an integer\n    between 1 and %d. The default is to start at submap number 1 or, if a map\n    was specified, at the first submap of that map.\n  -keys <left>-<right>-<up>-<down>-<fire> : Override the default key\n    bindings (cf. KeyCodes)\n", VERSION, msg, GAME_PERIOD, SYSVID_MAXZOOM, SYSVID_MAXZOOM, MAP_NBR_MAPS-1, MAP_NBR_SUBMAPS);
  63. #endif
  64.     exit(1);
  65. }
  66.  
  67. /*
  68.  * Get SDL key code
  69.  */
  70. static int
  71. sysarg_sdlcode(char *k)
  72. {
  73.   int i, result;
  74.  
  75.   i = 0;
  76.   result = 0;
  77.  
  78.   while (sdlcodes[i].code) {
  79.     if (!strcasecmp(sdlcodes[i].name, k)) {
  80.       result = sdlcodes[i].code;
  81.       break;
  82.     }
  83.     i++;
  84.   }
  85.  
  86.   return result;
  87. }
  88.  
  89. /*
  90.  * Scan key codes sequence
  91.  */
  92. int
  93. sysarg_scankeys(char *keys)
  94. {
  95.   char k[16];
  96.   int i, j;
  97.  
  98.   i = 0;
  99.  
  100.   j = 0;
  101.   while (keys[i] != '\0' && keys[i] != '-' && j + 1 < sizeof k) k[j++] = keys[i++];
  102.   if (keys[i++] == '\0') return -1;
  103.   k[j] = '\0';
  104.   syskbd_left = sysarg_sdlcode(k);
  105.   if (!syskbd_left) return -1;
  106.  
  107.   j = 0;
  108.   while (keys[i] != '\0' && keys[i] != '-' && j + 1 < sizeof k) k[j++] = keys[i++];
  109.   if (keys[i++] == '\0') return -1;
  110.   k[j] = '\0';
  111.   syskbd_right = sysarg_sdlcode(k);
  112.   if (!syskbd_right) return -1;
  113.  
  114.   j = 0;
  115.   while (keys[i] != '\0' && keys[i] != '-' && j + 1 < sizeof k) k[j++] = keys[i++];
  116.   if (keys[i++] == '\0') return -1;
  117.   k[j] = '\0';
  118.   syskbd_up = sysarg_sdlcode(k);
  119.   if (!syskbd_up) return -1;
  120.  
  121.   j = 0;
  122.   while (keys[i] != '\0' && keys[i] != '-' && j + 1 < sizeof k) k[j++] = keys[i++];
  123.   if (keys[i++] == '\0') return -1;
  124.   k[j] = '\0';
  125.   syskbd_down = sysarg_sdlcode(k);
  126.   if (!syskbd_down) return -1;
  127.  
  128.   j = 0;
  129.   while (keys[i] != '\0' && keys[i] != '-' && j + 1 < sizeof k) k[j++] = keys[i++];
  130.   if (keys[i] != '\0') return -1;
  131.   k[j] = '\0';
  132.   syskbd_fire = sysarg_sdlcode(k);
  133.   if (!syskbd_fire) return -1;
  134.  
  135.   return 0;
  136. }
  137.  
  138. /*
  139.  * Read and process arguments
  140.  */
  141. void
  142. sysarg_init(int argc, char **argv)
  143. {
  144.   int i;
  145.  
  146.   for (i = 1; i < argc; i++) {
  147.  
  148.     if (!strcmp(argv[i], "-fullscreen")) {
  149.       sysarg_args_fullscreen = 1;
  150.     }
  151.  
  152.     else if (!strcmp(argv[i], "-help") ||
  153.          !strcmp(argv[i], "-h")) {
  154.       sysarg_fail("help");
  155.     }
  156.  
  157.     else if (!strcmp(argv[i], "-speed")) {
  158.       if (++i == argc) sysarg_fail("missing speed value");
  159.       sysarg_args_period = atoi(argv[i]) - 1;
  160.       if (sysarg_args_period < 0 || sysarg_args_period > 99)
  161.     sysarg_fail("invalid speed value");
  162.     }
  163.  
  164.     else if (!strcmp(argv[i], "-keys")) {
  165.       if (++i == argc) sysarg_fail("missing key codes");
  166.       if (sysarg_scankeys(argv[i]) == -1)
  167.     sysarg_fail("invalid key codes");
  168.     }
  169.  
  170.     else if (!strcmp(argv[i], "-zoom")) {
  171.       if (++i == argc) sysarg_fail("missing zoom value");
  172.       sysarg_args_zoom = atoi(argv[i]);
  173.       if (sysarg_args_zoom < 1 || sysarg_args_zoom > SYSVID_MAXZOOM)
  174.     sysarg_fail("invalid zoom value");
  175.     }
  176.  
  177.     else if (!strcmp(argv[i], "-map")) {
  178.       if (++i == argc) sysarg_fail("missing map number");
  179.       sysarg_args_map = atoi(argv[i]) - 1;
  180.       if (sysarg_args_map < 0 || sysarg_args_map >= MAP_NBR_MAPS-1)
  181.     sysarg_fail("invalid map number");
  182.     }
  183.  
  184.     else if (!strcmp(argv[i], "-submap")) {
  185.       if (++i == argc) sysarg_fail("missing submap number");
  186.       sysarg_args_submap = atoi(argv[i]) - 1;
  187.       if (sysarg_args_submap < 0 || sysarg_args_submap >= MAP_NBR_SUBMAPS)
  188.     sysarg_fail("invalid submap number");
  189.     }
  190. #ifdef ENABLE_SOUND
  191.     else if (!strcmp(argv[i], "-vol")) {
  192.       if (++i == argc) sysarg_fail("missing volume");
  193.       sysarg_args_vol = atoi(argv[i]) - 1;
  194.       if (sysarg_args_submap < 0 || sysarg_args_submap >= SYSSND_MAXVOL)
  195.     sysarg_fail("invalid volume");
  196.     }
  197.  
  198.     else if (!strcmp(argv[i], "-nosound")) {
  199.       sysarg_args_nosound = 1;
  200.     }
  201. #endif
  202.     else if (!strcmp(argv[i], "-data")) {
  203.         if (++i == argc) sysarg_fail("missing data");
  204.         sysarg_args_data = argv[i];
  205.     }
  206.  
  207.     else {
  208.       sysarg_fail("invalid argument(s)");
  209.     }
  210.  
  211.   }
  212.  
  213.   /* this is dirty (sort of) */
  214.   if (sysarg_args_submap > 0 && sysarg_args_submap < 9)
  215.     sysarg_args_map = 0;
  216.   if (sysarg_args_submap >= 9 && sysarg_args_submap < 20)
  217.     sysarg_args_map = 1;
  218.   if (sysarg_args_submap >= 20 && sysarg_args_submap < 38)
  219.     sysarg_args_map = 2;
  220.   if (sysarg_args_submap >= 38)
  221.     sysarg_args_map = 3;
  222.   if (sysarg_args_submap == 9 ||
  223.       sysarg_args_submap == 20 ||
  224.       sysarg_args_submap == 38)
  225.     sysarg_args_submap = 0;
  226.  
  227. }
  228.  
  229. /* eof */
  230.  
  231.  
  232.  
  233.  
  234.  
  235.